#!/bin/sh
# save debug information to a partition...

if [ ! -f /tmp/sys.log ] ; then
  syslogd -O /tmp/sys.log
  klogd
fi
dmesg > /tmp/dmesg.txt
(
  for i in "cat /proc/cmdline" probedisk probepart_init \
    mount "cat /proc/partitions" df lsof ps
  do
    echo '------------------------'
    echo "      $i"
    echo '------------------------'
    $i
    echo; echo; echo
  done
) > /tmp/misc.log

#-----------------------------------------------------------

PARTITIONS=$(cat /proc/partitions | grep -E -o 'sd[a-z][1-9]|hd[a-z][1-9]|mmcblk[0-9]p[0-9]|nvme[0-9]n[0-9]p[0-9]' | tr '\n' ' ')

if [ "$(echo $PARTITIONS)" = "" ] ; then
	echo "Sorry, no partitions were detected"
	echo "Check the logs in /tmp"
	ls /tmp
	exit 1
fi

echo "We're going to save debug info for further analysis.."
echo
echo "Select one of the following partitions:"
echo " $PARTITIONS"
echo
echo -n "Type partition name: "
read xpart
mount_point=$(mountpartition $xpart)
[ $? -ne 0 ] && exit

mkdir -p ${mount_point}/zz_initrd_tmp
echo "* Copying /tmp/* to ${mount_point}/zz_initrd_tmp"
cp -r /tmp/* ${mount_point}/zz_initrd_tmp/
echo "* Unmounting $xpart"
umount ${mount_point}
echo
echo "Done.. reboot and find $xpart/zz_initrd_tmp"

killall syslogd 2>/dev/null
killall klogd 2>/dev/null

### END ###
